package lerry.display;

import flash.display.Bitmap;
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.net.URLRequest;

/**
 * ...
 * @author lerry
 */
class Image extends Sprite{
	var _loader:Loader;
	var _request:URLRequest;
	var _content:Bitmap;

	var _isRolloverX:Bool;
	var _isRolloverY:Bool;
	
	public var url(default,set_url):String;
	
	function set_url(value:String) :String {
		url = value;
		if (_content != null) {
			if (contains(_content)) {
				removeChild(_content);
			}
			_content.bitmapData.dispose();
			_content = null;
			
		}
		if (_loader == null) {
			_loader = new Loader();
		}else {
			_loader.unloadAndStop();
		}
		if (_request == null) {
			_request = new URLRequest();
		}
		_request.url = url;
		_loader.load(_request);
		_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);
		return value;
	}
	
	function onLoadComplete(e:Event):Void {
		_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onLoadComplete);
		_content = cast(_loader.content,Bitmap);
		addChild(_content);
		if (_isRolloverX) {
			_isRolloverX = false;
			_content.scaleX *= -1;
			_content.x == 0?_content.x = _content.width:_content.x=0;
		}
		if (_isRolloverY) {
			_isRolloverY = false;
			_content.scaleY *= -1;
			_content.y == 0?_content.y = _content.height:_content.y=0;
		}
	}
	/**
	 * 在x轴方向上翻转
	 */
	public function rolloverX():Void {
		if (_content != null) {
			_content.scaleX *= -1;
			_content.x == 0?_content.x = _content.width:_content.x=0;
		}else {
			_isRolloverX = true;
		}
	}
	/**
	 * 在y轴方向上翻转
	 */
	public function rolloverY():Void {
		if (_content != null) {
			_content.scaleY *= -1;
			_content.y == 0?_content.y = _content.height:_content.y=0;
		}else {
			_isRolloverY = true;
		}
	}
	public function new() {
		super();
	}
	
	
}